// import { getUSBPrinters } from "./printAdapterManager";
import { WinUSB } from "./winUsbAdapter";

const escpos = require('escpos');
const os = require('os');
const ifaces = os.networkInterfaces();

/**
 * 打印数组中的每行数据
 * @param {*} lines 
 */
export async function printLines(lines) {
    let p = new WinUSB(-1, -1);
    let usbPrinters = await p.findPrinter() as { pid: string; vid: string }[];
    let usbDevice = usbPrinters[0];

    let device = new WinUSB(usbDevice.pid, usbDevice.vid);

    // const device = new escpos.USB();
    const options = { encoding: "GB18030" };
    const printer = new escpos.Printer(device, options);
    device.open(() => {
        lines.forEach(line => {
            const font = line.font ? line.font : 'A';
            const align = line.align ? line.align : 'LT';
            const style = line.style ? line.style : 'NORMAL';
            const size = line.size ? line.size : { w: 1, h: 1 };
            printer.font(font).style(style).align(align).size(size.w, size.h).text(line.text);
        });
        printer.feed(4).cut().close();
    });
}
/**
 * 获取当前的操作系统和IP地址
 */
export function getSystemInfo() {
    const platform = os.platform();
    let ipAddress: any[] = [];
    Object.keys(ifaces).forEach(function (ifname) {
        ifaces[ifname].forEach(function (iface) {
            if ('IPv4' == iface.family && !iface.internal && iface.address != '127.0.0.1') {
                ipAddress.push(iface.address);
            }
        });
    });
    const systemInfo = {
        platform,
        ipAddress
    };
    return systemInfo;
}